/*
 * berton
 */
package com.pitop.supplier.service;

import com.pitop.core.entity.Page;
import com.pitop.core.exceptions.BaseException;
import com.pitop.core.exceptions.SupplierException;
import com.pitop.supplier.dao.SupplierDAO;
import com.pitop.supplier.entity.SupplierState;
import com.pitop.supplier.entity.Supplier;
import com.pitop.core.base.BaseDAO;
import com.pitop.core.base.BaseSVImpl;
import com.alibaba.fastjson.serializer.SerializerFeature;
import lombok.extern.slf4j.Slf4j;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
import java.util.HashMap;
import java.util.Date;
import java.util.Map;
import org.apache.ibatis.session.RowBounds;
import com.baidu.fsg.uid.UidGenerator;

@Service("supplierSV")
@Slf4j
public class SupplierSVImpl extends BaseSVImpl<Supplier, Long> implements SupplierSV {

    @Autowired
    private SupplierDAO supplierDAO;

    @Resource
    private UidGenerator uidGenerator;

    @Override
    protected BaseDAO getBaseDAO() {
        return supplierDAO;
    }

    /**
    * 保存account对象
    *
    * @param entity 实体
    * @throws BaseException
    */
    @Override
    public void save(Supplier entity) throws BaseException {
        entity.setCode(String.valueOf(uidGenerator.getUID()));
        entity.setCreateTime(new Date());
        entity.setUpdateTime(new Date());
        super.save(entity);
    }

    /**
     * 加载一个对象Supplier
     * @param id * @param code 编码
     * @return Supplier
     */
    @Override
    public Supplier load(java.lang.Long id,java.lang.String code) {
           if(id==null&&code==null){
                 throw new SupplierException(BaseException.BaseExceptionEnum.Ilegal_Param);
           }

            Map<String,Object> param=new HashMap<>();
            param.put("id",id);
            param.put("code",code);
            return supplierDAO.load(param);
    }
    /**
     * 加载一个对象Supplier 通过id
     * @param id 
     * @return Supplier
     */
    @Override
    public Supplier loadById(java.lang.Long id) {
            if(id==null){
                 throw new SupplierException(BaseException.BaseExceptionEnum.Ilegal_Param);
            }
            return supplierDAO.loadById(id);
    }

    /**
     * 加载一个对象Supplier 通过code
     * @param code 编码
     * @return Supplier
     */
    @Override
    public Supplier loadByCode(java.lang.String code) {
            if(code==null){
                 throw new SupplierException(BaseException.BaseExceptionEnum.Ilegal_Param);
            }
            return supplierDAO.loadByCode(code);
    }


    /**
    * 根据主键id,oldStates 共同更新 Supplier 的状态到newState状态
    *
    * @param id 
    * @param newState 新状态
    * @param oldStates 旧状态集合
    */
    @Override
    public void updateStateById(java.lang.Long id,SupplierState newState,SupplierState... oldStates){
        if(id==null){
            throw new SupplierException(BaseException.BaseExceptionEnum.Ilegal_Param);
        }
        if(newState==null){
            throw new SupplierException(BaseException.BaseExceptionEnum.Ilegal_Param);
        }
        if(oldStates==null){
            throw new SupplierException(BaseException.BaseExceptionEnum.Ilegal_Param);
        }
        supplierDAO.updateStateById(id,new Date(),newState,oldStates);
    }
    /**
    * 根据主键code,oldStates 共同更新 Supplier 的状态到newState状态
    *
    * @param code 编码
    * @param newState 新状态
    * @param oldStates 旧状态集合
    */
    @Override
    public void updateStateByCode(java.lang.String code,SupplierState newState,SupplierState... oldStates){
        if(code==null){
            throw new SupplierException(BaseException.BaseExceptionEnum.Ilegal_Param);
        }
        if(newState==null){
            throw new SupplierException(BaseException.BaseExceptionEnum.Ilegal_Param);
        }
        if(oldStates==null){
            throw new SupplierException(BaseException.BaseExceptionEnum.Ilegal_Param);
        }
        supplierDAO.updateStateByCode(code,new Date(),newState,oldStates);
    }

    /**
    * 根据主键id 更新 Supplier 的状态到另一个状态
    *
    * @param id 
    * @param state 状态
    */
    @Override
    public void updateById(java.lang.Long id,SupplierState state){
            if(id==null){
               throw new SupplierException(BaseException.BaseExceptionEnum.Ilegal_Param);
            }
          supplierDAO.updateById(id,state,new Date());
    }
    /**
    * 根据主键code 更新 Supplier 的状态到另一个状态
    *
    * @param code 编码
    * @param state 状态
    */
    @Override
    public void updateByCode(java.lang.String code,SupplierState state){
            if(code==null){
               throw new SupplierException(BaseException.BaseExceptionEnum.Ilegal_Param);
            }
          supplierDAO.updateByCode(code,state,new Date());
    }


    /**
     * 删除对象Supplier
     * @param id * @param code 编码
     * @return Supplier
     */
    @Override
    public void delete(java.lang.Long id,java.lang.String code) {
            if(id==null&&code==null){
                throw new SupplierException(BaseException.BaseExceptionEnum.Ilegal_Param);
            }
            Map<String,Object> param=new HashMap<>();
            param.put("id",id);
            param.put("code",code);
            supplierDAO.delete(param);
    }



    /**
    * 查询Supplier分页
    *
    * @param supplier  对象
    * @param offset 查询开始行
    * @param limit  查询行数
    * @return List<Supplier>
    */
    @Override
    public List<Supplier> list(Supplier supplier, int offset, int limit) {
            if (offset < 0) {
               offset = 0;
            }

            if (limit < 0) {
               limit = Page.limit;
            }

            Map<String, Object> map = null;
            if (supplier != null) {
               map = JSON.parseObject(JSON.toJSONString(supplier, SerializerFeature.WriteDateUseDateFormat));
            } else {
               map = new HashMap<>();
            }
        return supplierDAO.list(map, new RowBounds(offset, limit));
    }

    @Override
    public  int count(Supplier supplier){
        Map<String, Object> map = null;
        if (supplier != null) {
           map = JSON.parseObject(JSON.toJSONString(supplier, SerializerFeature.WriteDateUseDateFormat));
        } else {
           map = new HashMap<>();
        }
       return supplierDAO.count(map);
     }

    /**
     * 查询Supplier分页
     *
     * @param id  * @param code  编码
     * @param offset 查询开始行
     * @param limit  查询行数
     * @return List<Supplier>
     */
    @Override
    public List<Supplier> list(java.lang.Long id,java.lang.String code,int offset, int limit) {
            if (offset < 0) {
              offset = 0;
            }

            if (limit < 0) {
              limit = Page.limit;
            }
            Map<String,Object> param=new HashMap<>();
            param.put("id",id);
            param.put("code",code);
            return supplierDAO.list(param,new RowBounds(offset, limit));
    }

    @Override
    public int count(java.lang.Long id,java.lang.String code) {
            Map<String,Object> param=new HashMap<>();
            param.put("id",id);
            param.put("code",code);
            return supplierDAO.count(param);
    }

}
